04. Collections

Collections

ND079 C1 L5 A04 Collections List

Collections are a set of data structures that were introduced in Java 5.0 to solve problems with consistency between data structures and to address performance issues. They were developed with the following goals in mind:

  • High performance/efficiency.
  • High degree of interoperability. The new data structures all needed to behave similarly to each other. In practice, this means that Collections data structures implement similar interfaces and extend similar abstract classes.

  • Integrate with existing APIs seamlessly. The Java framework had to extend and adapt to the new data structures easily. They needed to integrate the new data structures seamlessly with little disruption to the existing API.


Now that we have learned about why collections were created. Let’s take a look at what they are and how to use them in InteliJ.

Syntax Example

The Collections framework consists of several different data structure classes like List, Maps and Queues. In addition, there are utility classes like Collections and Arrays that provide methods for sorting and creating empty lists. Below is an example in which we create a List of type String and then add and remove data.

** Notice: ** We are creating an instance of List but we are using an ArrayList to insatiate it. This is because List is an Interface and we must use a concrete class to instantiate the object.

List<String> myList = new ArrayList<String>();

myList.add("one");
myList.add("two");
myList.add("three");

//We could also remove the element "one" by using the index 0 instead of the value "one".
myList.remove("one");

Which of the statements below are true about collections?

(Select all that apply.)

SOLUTION:
  • Collection inherits from the Iterable interface.
  • Map, List, Set and Queue all implement the Collection interface.
  • Collection is an Interface.